package com.dxp.plugins;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.UUIDUtil;
import com.dxp.utils.StrUtils;

import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @Classname AuditSysIntPluginImpl
 * @Description TODO
 * @Date 2020/3/19 7:24
 * @Created by fpc
 */
public class AuditSysIntPluginImpl implements CustomMethodPlugin {
    @Override
    public Object beforeInvoke(Map<String, Object> map) {
        String auditStatus = String.valueOf(map.get("dxSysIntApply.auditStatus"));
//        String reuestPwd = "cms";
        String reuestPwd = String.valueOf(map.get("dxSysIntApply.reuestPwd"));
        if(Validator.isNull(auditStatus)){
            throw new BusinessException("审核状态或者秘钥不能为空。");
        }
        String requestUrl="/data/";
        if("90".equals(auditStatus)){
            String w_infoId = String.valueOf(map.get("dxSysIntApply.w_infoId"));
            Map<String,Object> whereMap = new HashMap<>();
            whereMap.put("dxSysIntApply.w_infoId",w_infoId);
            ResultEntity resultEntity1 = IusPluginUtil.getInstance().select("obj", "s_dxSysIntApply", whereMap);
            if(!Validator.isEmpty(resultEntity1.getEntity())){
                Map dxSysIntApply = (Map)resultEntity1.getEntity();
                Map<String,Object> saveMap = new HashMap<>();
                saveMap.put("dxSysIntRelation.sysEname",dxSysIntApply.get("sysEname"));
                saveMap.put("dxSysIntRelation.applyCompany",dxSysIntApply.get("applyCompany"));
                saveMap.put("dxSysIntRelation.exchangeMode",dxSysIntApply.get("exchangeMode"));
                saveMap.put("dxSysIntRelation.fromServerId",dxSysIntApply.get("fromServerId"));
//                saveMap.put("dxSysIntRelation.fromInterfaceName",dxSysIntApply.get("fromInterfaceName"));
                saveMap.put("dxSysIntRelation.fromInterfaceCode",dxSysIntApply.get("fromInterfaceCode"));
                String requestCode = dxSysIntApply.get("sysEname")+ StrUtils.generateNonceStr(6);//系统编号+随机字符串
                saveMap.put("dxSysIntRelation.requestCode",requestCode);
//                String reuestPwd = UUIDUtil.getUUID("iussoft",32);
                requestUrl = requestUrl+requestCode+"/";
                if(Validator.isNotNull(reuestPwd)){
                    saveMap.put("dxSysIntRelation.reuestPwd",reuestPwd);
//                    requestUrl = requestUrl+reuestPwd;
                }
                saveMap.put("dxSysIntRelation.requestUrl",requestUrl);
                ResultEntity resultEntity = IusPluginUtil.getInstance().saveInvoke("dxp", "i_dxSysIntRelation", saveMap);

                map.put("dxSysIntApply.requestUrl",requestUrl);
            }

        }
        return null;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }



}
